﻿Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient

Public Class BuildConnection
    Inherits System.Web.UI.Page


    Protected Sub btnConnect_click(sender As Object, e As EventArgs) Handles btnConnect.Click
        Dim cnn As SqlConnection
        Dim mySQLcnn As MySqlConnection
        Dim builder As New SqlConnectionStringBuilder
        Dim mySQLbuilder As New MySqlConnectionStringBuilder
        Select Case Me.DropDownList1.SelectedIndex
            Case 0
                If Trim(Me.txtPort.Text) <> "" Then
                    builder("Data Source") = Me.txtServer.Text & "," & Me.txtPort.Text
                Else
                    builder("Data Source") = Me.txtServer.Text
                End If
                builder("Initial Catalog") = Me.txtDataBase.Text
                builder("Integrated Security") = Me.CheckBox1.Checked.ToString
                'builder("Port") = Me.txtPort.Text

                If Me.CheckBox1.Checked = False Then
                    builder("user ID") = Me.txtUserName.Text
                    builder("Password") = Me.txtPassword.Text
                End If
                Me.lblConnectionString.Text = Me.lblConnectionString.Text
                builder.AsynchronousProcessing = True
                cnn = New SqlConnection(builder.ConnectionString)
                Try
                    cnn.Open()
                    cnn.Close()
                    Session("myConnection") = cnn.ConnectionString
                    Me.lblConnectionString.ForeColor = Drawing.Color.Blue
                    Me.lblConnectionString.Text = "Connection looks good!"

                Catch ex As Exception
                    Me.lblConnectionString.ForeColor = Drawing.Color.Red
                    Me.lblConnectionString.Text = "An Error Occured!" & ex.Message
                    Session("myConnection") = ""
                End Try

            Case 1
                mySQLbuilder.Server = Me.txtServer.Text
                If Trim(Me.txtPort.Text) <> "" Then
                    mySQLbuilder.Port = Me.txtPort.Text
                End If
                mySQLbuilder.Database = txtDataBase.Text
                mySQLbuilder.IntegratedSecurity = Me.CheckBox1.Checked.ToString
                If Me.CheckBox1.Checked = False Then
                    mySQLbuilder.UserID = Me.txtUserName.Text
                    mySQLbuilder.Password = Me.txtPassword.Text
                End If
                Me.lblConnectionString.Text = mySQLbuilder.ConnectionString
                mySQLcnn = New MySqlConnection(mySQLbuilder.ConnectionString)
                Try
                    mySQLcnn.Open()
                    mySQLcnn.Close()
                    Session("myConnection") = Me.lblConnectionString.Text
                    Me.lblConnectionString.ForeColor = Drawing.Color.Blue
                    Me.lblConnectionString.Text = "Connection looks good!"

                Catch ex As Exception
                    Me.lblConnectionString.ForeColor = Drawing.Color.Red
                    Me.lblConnectionString.Text = "An Error Occured!" & ex.Message
                    'Session("myConnection") = ""
                End Try

        End Select


    End Sub

    Protected Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If Me.CheckBox1.Checked Then
            Me.txtUserName.Enabled = False
            Me.txtPassword.Enabled = False
        Else
            Me.txtUserName.Enabled = True
            Me.txtPassword.Enabled = True
        End If
    End Sub


End Class